package com.liuxing.javaSpringBoot.modules.account.controller;

import com.github.pagehelper.PageInfo;
import com.liuxing.javaSpringBoot.modules.account.entity.Role;
import com.liuxing.javaSpringBoot.modules.account.service.RoleService;
import com.liuxing.javaSpringBoot.modules.common.entity.Result;
import com.liuxing.javaSpringBoot.modules.common.entity.SearchBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * @File RoleController.java
 * @Auth liuxing
 * @Date 2021/8/23 16:48
 * @Email liuxing997@foxmail.com
 */
@RestController
@RequestMapping("/api")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 添加角色信息
     * 127.0.0.1/api/role --- post
     * {"roleName":"管理员"}
     */
    @PostMapping(value = "/role", consumes = "application/json")
    public Result<Role> insertRole(@RequestBody Role role) {
        return roleService.insertRole(role);
    }

    /**
     * 根据角色ID修改角色信息
     * 127.0.0.1/api/role --- put
     * {"id":2,"roleName":"普通管理员"}
     */
    @PutMapping(value = "/role", consumes = "application/x-www-form-urlencoded")
    public Result<Role> updateRoleById(@ModelAttribute Role role) {
        return roleService.updateRoleById(role);
    }

    /**
     * 根据角色ID删除角色
     * 127.0.0.1/api/role/1 --- delete
     */
    @DeleteMapping(value = "/role/{id}")
    public Result<Object> deleteRoleByRoleId(@PathVariable int id) {
        return roleService.deleteRoleByRoleId(id);
    }

    /**
     * 根据角色ID查询角色信息
     * 127.0.0.1/api/role/3 --- get
     */
    @GetMapping("/role/{id}")
    public Role getRoleByRoleId(@PathVariable int id) {
        return roleService.getRoleByRoleId(id);
    }

    /**
     * 根据用户ID查询角色信息
     * 127.0.0.1/api/roles/3 --- get
     */
    @GetMapping("/roles/{userId}")
    public Result<Object> getRoleByUserId(@PathVariable int userId) {
        return new Result<>(Result.ResultCode.SUCCESS.code, "查询成功！",roleService.getRolesByUserId(userId));
    }

    /**
     * 查询所有角色信息
     * 127.0.0.1/api/roles --- get
     */
    @GetMapping("/roles")
    public List<Role> getAllRole() {
        return roleService.getAllRole();
    }

    /**
     * 批量插入角色信息
     * 127.0.0.1/api/batch/roles  --- post
     * [{"roleName":"普通用户"},{"roleName":"VIP用户"}]
     */
    @PostMapping(value = "/batch/roles",consumes = "application/json")
    public Result<Object> insertRoles(@RequestBody List<Role> roles) {
        return roleService.insertRoles(roles);
    }

    /**
     * 分页查询角色信息
     * 127.0.0.1/api/roles --- post
     * {"currentPage":1,"pageSize":3,"orderBy":"role_name","direction":"desc"}
     */
    @PostMapping(value = "/roles",consumes = "application/json")
    public PageInfo<Role> getRolesBySearchBean(@RequestBody SearchBean searchBean) {
        return roleService.getRolesBySearchBean(searchBean);
    }
}
